package com.yuedao.sender.service1;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.ydx.protobuf.OTIpcDef;

import com.yuedao.sender.model.OperateLogin;
import com.yuedao.sender.model.OrderCancel;
import com.yuedao.sender.model.OrderCreate;
import com.yuedao.sender.model.OrderMatch;
import com.yuedao.sender.service.OrderCancelRepository;
import com.yuedao.sender.service.OrderCreateRepository;
import com.yuedao.sender.service.OrderMatchRepository;
/**
 * @author xfm
 * 2018年5月16日
 */
@Service
public class OrderService extends BaseService {

	//@Value("${target.ordercreate.url}")
	private String createURL ="/order/create";

	//@Value("${target.ordermatch.url}")
	private String matchURL="/order/match";

	//@Value("${target.ordercancel.url}")
	private String cancelURL = "/order/cancel";


	/**
	 * A.5.1 订单发起接口
	 */
	public String sendOrderCreate(List<OrderCreate> list ) {
		
		if (list.size() > 0) {
			OTIpcDef.OTIpc.Builder builder = OTIpcDef.OTIpc.newBuilder()
					.setCompanyId(companyId).setSource(companySource)
					.setIPCType(OTIpcDef.IpcType.orderCreate);
			for (OrderCreate data : list) {
				builder.addOrderCreate(OrderCreate.convert(data));
			}

			OTIpcDef.OTIpc otIpc = builder.build();

			return super.compressAndSend(createURL, otIpc);
		}
		return null;
	}

	/**
	 * A.5.2 订单成功接口
	 */
	public String sendOrderMatch(List<OrderMatch> list) {
		
		if (list.size() > 0) {
			OTIpcDef.OTIpc.Builder builder = OTIpcDef.OTIpc.newBuilder()
					.setCompanyId(companyId).setSource(companySource)
					.setIPCType(OTIpcDef.IpcType.orderMatch);
			for (OrderMatch data : list) {
				builder.addOrderMatch(OrderMatch.convert(data));
			}
			OTIpcDef.OTIpc otIpc = builder.build();
			return super.compressAndSend(matchURL, otIpc);
		}
		return null;
	}

	/**
	 * A.5.3 订单撤销接口
	 */
	public String sendOrderCancel(List<OrderCancel> list ) {

		if (list.size() > 0) {
			OTIpcDef.OTIpc.Builder builder = OTIpcDef.OTIpc.newBuilder()
					.setCompanyId(companyId).setSource(companySource)
					.setIPCType(OTIpcDef.IpcType.orderCancel);
			for (OrderCancel data : list) {
				builder.addOrderCancel(OrderCancel.convert(data));
			}

			OTIpcDef.OTIpc otIpc = builder.build();

			return super.compressAndSend(cancelURL, otIpc);
		}
		    return null;
	}

}
